package com.zzyl.serve.service.impl;

import java.util.List;

import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author wsq
 * @date 2025-03-02
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id) {
        // 1. 根据id查询护理计划
        NursingPlan plan = getById(id);
        // 2. 属性拷贝 nursingPlan到 nursingPlanVo中
        // 拷贝时的注意事项: 属性名 + 数据类型 都要一致,否则尤其是id会拷贝失败，并赋值失败
        NursingPlanVo vo = new NursingPlanVo();
        BeanUtils.copyProperties(plan, vo);
        // 3. 根据护理计划id查询关联的所有护理项目计划表的list
        List<NursingProjectPlanVo> projectPlans = nursingProjectPlanMapper.selectByPlanId(id);
        // 4. 将list赋值给nursingPlanVo
        vo.setProjectPlans(projectPlans);
        // 5. 返回nursingPlanVo
        return vo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto dto) {
        // 保存护理计划
        // 属性拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        // 新增护理计划
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        // 批量新增计划中的项目
        int count = nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), nursingPlan.getId());
        return count == 0 ? 0 : 1;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto dto) {
        //1.属性拷贝：dto拷贝到nursingPlan中
        NursingPlan plan = new NursingPlan();
        BeanUtils.copyProperties(dto, plan);
        //2.判断dto中的项目列表是否为空,如果不为空，则先删除护理计划与护理项目的关系，然后重新批量添加
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        if(projectPlans != null && projectPlans.size() > 0){
            //2.1 删除护理计划对应的护理项目列表
            nursingProjectPlanMapper.deleteByPlanId(plan.getId());
            //2.2 批量保存护理计划对应的护理项目列表
            nursingProjectPlanMapper.batchInsert(projectPlans, plan.getId());
        }
        //3.修改护理计划
        return nursingPlanMapper.updateNursingPlan(plan);
        //4.注意事务控制
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Integer id) {
        // 1. 删除关联表数据
        nursingProjectPlanMapper.deleteByPlanId(id);
        // 2. 删除计划表数据
        return removeById(id) ? 1 : 0;
        // 3. 注意事务控制
    }

}